<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
		
        <div class="container mt-1">
          <el-form inline size="small">
			<div class="flex items-center search_form">
				<div>
					联系人：
					<el-input
					v-model="searchForm.name"
					placeholder="请输入联系人"
					style="width: 200px; margin-right: 10px;"
					size="small"
					></el-input>
				</div>
				<div>
					联系方式：
					<el-input
					v-model="searchForm.phone"
					placeholder="请输入联系方式"
					style="width: 200px; margin-right: 10px;"
					size="small"
					></el-input>
				</div>
				<div>
					省：
					<el-input
					v-model="searchForm.province"
					placeholder="请输入省"
					style="width: 200px; margin-right: 10px;"
					size="small"
					></el-input>
				</div>
				<div>
					市：
					<el-input
					v-model="searchForm.city"
					placeholder="请输入市"
					style="width: 200px; margin-right: 10px;"
					size="small"
					></el-input>
				</div>
				<div>
					区：
					<el-input
					v-model="searchForm.area"
					placeholder="请输入区"
					style="width: 200px; margin-right: 10px;"
					size="small"
					></el-input>
				</div>
				<div>
					添加时间：
					<el-date-picker
					v-model="time"
					type="daterange"
					value-format="yyyy-MM-dd"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期"
					style=" margin-right: 10px;"
					size="small"
					></el-date-picker>
				</div>
				
				 <div style="margin-left: 10px;">
					<el-button type="primary" size="mini" @click="mySearch">搜索</el-button>
					<el-button size="mini" @click="reset">重置</el-button>
					<el-button type="primary" size="mini" @click="addBtn">添加</el-button>
				 </div>
			</div>
			
           
			
           
          </el-form>
        </div>
      </div>
      <el-table
        :data="list"
        style="width: 100%"
        size="mini"
        height="600px"
        :highlight-current-row="true"
        :header-cell-style=" {fontWeight:'bold'}"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="50"
        />
        <el-table-column label="联系人"
	  					align="center"
                         prop="name"
                         min-width="100"
                         :show-overflow-tooltip="true">
		
        </el-table-column>
		<el-table-column label="联系方式"
		                 prop="phone"
						 align="center"
		                 min-width="100"
		                 :show-overflow-tooltip="true">
		</el-table-column>
		<el-table-column label="添加时间"
		                 prop="add_time"
						width="200"
						 align="center">
		</el-table-column>
		<el-table-column label="修改时间"
		                 prop="update_time"
						 width="200"
						 align="center">	
		</el-table-column>
		<el-table-column label="省市区"
		                 prop="province"
						 width="200"
						 align="center">
			<template slot-scope="scope">
				{{scope.row.province}}-{{scope.row.city}}-{{scope.row.area}}
			</template>
		</el-table-column>
		<el-table-column label="详细信息"
		                 prop="address_info"
		                 min-width="200"	
						 align="center"
		                 :show-overflow-tooltip="true">
		</el-table-column>
		<el-table-column label="备注"
		                 prop="desc"
						 align="center"
		                 :show-overflow-tooltip="true">
		</el-table-column>
		
        <el-table-column label="操作" min-width="130" fixed="right">
          <template slot-scope="scope">
			<el-button type="text" @click="editRow(scope.row)">编辑</el-button>
			<el-button type="text" @click="delRow(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
	  <div class="page">
	              <el-pagination
	                      background
	                      @size-change="pageChange"
	                      @current-change="handleCurrentChange"
	                      :current-page="page"
	                      :page-size="pageSize"
	                      layout="total, sizes, prev, pager, next, jumper"
	                      :total="page_totle">
	              </el-pagination>
	          </div>
    </el-card>
	
	<!-- 新增角色 -->
	<el-dialog
	  :title="title"
	  :visible.sync="formShow"
	  :close-on-click-modal="false"
	  width="800px">
	  <el-form ref="form" :model="form" label-width="120px">
		  <el-form-item label="姓名" prop="age">
			<el-input v-model="form.name" placeholder="请输入姓名" size="small"></el-input>
		  </el-form-item>
		  <el-form-item label="联系方式" prop="phone">
			<el-input v-model="form.phone" placeholder="请输入联系方式" size="small"></el-input>
		  </el-form-item>
		  <el-form-item label="省市区">
			<el-cascader style="width:100%;" :options="areaOpt" 
				size="small"
  				v-model="selectedOptions" @change="chooseArea"></el-cascader>
		  </el-form-item>
		  <el-form-item label="详细信息" prop="address_info">
			<el-input v-model="form.address_info" placeholder="请输入详细地址信息" size="small"></el-input>
		  </el-form-item>
		  <el-form-item label="备注" prop="desc">
			<el-input v-model="form.desc" placeholder="请输入备注" size="small"></el-input>
		  </el-form-item>
		  
	  </el-form>
	  <span slot="footer" class="dialog-footer">
	    <el-button size="mini" @click="formShow = false">取 消</el-button>
	    <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
	  </span>
	</el-dialog>
  </div>
</template>
<script>
import { regionData ,codeToText} from 'element-china-area-data'
  export default {
    data() {
      return {
        title:'新增',
		form:{
			id:null,
			province:"",
			city:"",
			area:"",
			phone:"",
			name:"",
			province_code:"",
			city_code:"",
			area_code:"",
			address_info:"",
			desc:""
		},
		selectedOptions:[],
		areaOpt: regionData,
		list:[],
		page:1,
		pageSize:10,
		searchForm:{
			province:"",
			city:"",
			area:"",
			phone:"",
			name:"",
		},
		time:[],
      }
    },
    mounted() {
		this.lodeList()
    },
    methods: {
		addBtn(){
			this.form = {
				id:null,
				province:"",
				city:"",
				area:"",
				phone:"",
				name:"",
				province_code:"",
				city_code:"",
				area_code:"",
				address_info:"",
				desc:""
			}
			this.title = '新增'
			this.selectedOptions = []
			this.formShow = true
		},
		chooseArea(value) {//区域地址改变
			this.form.province = codeToText[value[0]]
			this.form.city = codeToText[value[1]]
			this.form.area = codeToText[value[2]]
		},

		reset(){
			this.searchForm = {
				province:"",
				city:"",
				area:"",
				phone:"",
				name:"",
			}
			this.time = []
			this.mySearch()
		},
		
		editRow(row,type){
			
			this.type = type
			this.title = '编辑'
			for(let i in this.form){
				this.form[i] = row[i]
			}
			this.selectedOptions = [this.form.province_code,this.form.city_code,this.form.area_code]
			this.formShow = true
		
		},
		
		confirm(){
			
			this.form.province_code = this.selectedOptions[0]
				this.form.city_code = this.selectedOptions[1]
				this.form.area_code = this.selectedOptions[2]
		    this.$api.post('/admin/addEditAddress',this.form).then(res=>{
		    	  if(res.code==200){
					  this.formShow = false
		    	  	this.lodeList()
						this.$message({
								type: 'success',
								message: res.msg
							})
		    	  }else{
						this.$message({
								type: 'error',
								message: res.msg
							})
				  }
		    })
		},
		delRow(id){
			this.$confirm('确定删除', '提示', {
					  confirmButtonText: '确定',
					  cancelButtonText: '取消',
					  type: 'warning'
					}).then(() => {
						let params = {
							dd:{id}
						}
					  this.$api.post('/admin/delAddress',params).then(res=>{
					  	  if(res.code==200){
					  	  	this.lodeList()
					  	  }
					  })
			  })
	   },
      lodeList(){
		  if(this.time.length>0){
			this.searchForm.start_time = this.time[0]
			this.searchForm.end_time = this.time[1]
		  }
		  let params = {
		          	page:this.page,
		          	pageSize:this.pageSize,
					searchForm:this.searchForm
		    }
		 
		  this.$api.post('/admin/getAddressList',params).then(res=>{
			  let data = res.data
			  this.page_totle = data.count
			  this.list = data.result
		  })
	  }
      
    }
  }
</script>
